package hdelosr

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
	"wu-yan-zhu/cache"
	"wu-yan-zhu/models"
	"wu-yan-zhu/pkg"
)

func Dl(c *gin.Context) {
	name := c.PostForm("name")
	passes := c.PostForm("passes")
	if name == "" || passes == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字不能为空",
		})
		return
	}
	redis_key := "redis_dl" + name
	qw, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if qw >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误3次,禁止登录",
		})
		return
	}
	var user models.User
	err := models.DB.Where("name=?", name).First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询用户失败",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户不存在",
		})
		return
	}
	if user.Passes != pkg.MD5(passes) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if qw == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误3次,限制2小时以后才能登录",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": user,
	})
	return
}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offend := (page - 1) * size

	//var count int64
	//models.DB.Model(&models.CarOrder{}).Count(&count)
	/*
		SELECT
		car_orders.id as OrderId,
		cats.cat_no as CatNo,
		cats.cat_name as CatName,
		cats.cat_code as CatCode,
		car_orders.order_no as OrderNo,
		car_orders.order_from as OrderFrom,
		car_orders.order_to as OrderTo,
		car_orders.order_good as OrderGood
		FROM
		car_orders
		  LEFT JOIN cats ON cats.id=car_orders.cid
	*/
	var list []models.CarOrderList

	redis_key := "redis_list" + strconv.Itoa(page)
	po, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if po == "" {
		models.DB.Table("car_orders").
			Select("car_orders.id as OrderId",
				"cats.cat_no as CatNo",
				"cats.cat_name as CatName",
				"cats.cat_code as CatCode",
				"car_orders.order_no as OrderNo",
				"car_orders.order_from as OrderFrom",
				"car_orders.order_to as OrderTo",
				"car_orders.order_good as OrderGood",
			).
			Joins("LEFT JOIN cats ON cats.id=car_orders.cid").
			Offset(offend).
			Limit(size).
			Find(&list)
		jsonList, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonList, time.Hour*2)
	} else {
		json.Unmarshal([]byte(po), &list)
	}
	var count int64
	models.DB.Model(&models.CarOrder{}).Count(&count)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": gin.H{
			"count": count,
			"list":  list,
		},
	})
	return
}
func Xq(c *gin.Context) {
	id := c.Query("id")
	var list models.CarOrderList

	redis_key := "redis_xq" + id
	po, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if po == "" {
		models.DB.Table("car_orders").
			Select("car_orders.id as OrderId",
				"cats.cat_no as CatNo",
				"cats.cat_name as CatName",
				"cats.cat_code as CatCode",
				"car_orders.order_no as OrderNo",
				"car_orders.order_from as OrderFrom",
				"car_orders.order_to as OrderTo",
				"car_orders.order_good as OrderGood",
			).
			Joins("LEFT JOIN cats ON cats.id=car_orders.cid").
			Where("car_orders.id=?", id).
			Find(&list)
		jsonList, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonList, time.Hour*2)
	} else {
		json.Unmarshal([]byte(po), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}
func Add(c *gin.Context) {
	OrderNo := c.PostForm("order_no")
	OrderFrom := c.PostForm("order_from")
	OrderTo := c.PostForm("order_to")
	OrderGood := c.PostForm("order_good")
	Cid, _ := strconv.Atoi(c.PostForm("cid"))
	if OrderNo == "" || OrderFrom == "" || OrderTo == "" || OrderGood == "" || Cid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.CarOrder{
		OrderNo:   OrderNo,
		OrderFrom: OrderFrom,
		OrderTo:   OrderTo,
		OrderGood: OrderGood,
		Cid:       Cid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}

	redis_key := "redis_add" + strconv.Itoa(int(data.ID))
	var add models.CarOrder
	models.DB.Where("id=?", data.ID).First(&add)
	marshal, _ := json.Marshal(add)
	cache.Rdb.Set(cache.Ctx, redis_key, marshal, time.Hour*2)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func Del(c *gin.Context) {
	id := c.Query("id")

	redis_key := "redis_add" + id
	cache.Rdb.Del(cache.Ctx, redis_key)

	models.DB.Where("id=?", id).Delete(&models.CarOrder{})
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "删除成功",
	})
	return
}
